package iscript.uiFramework.components.panel
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import iscript.uiFramework.components.container.Box;
	import iscript.uiFramework.components.container.Container;
	import iscript.uiFramework.components.container.IBox;
	import iscript.uiFramework.components.container.IContainer;
	import iscript.uiFramework.components.container.ScrollBox;
	import iscript.uiFramework.components.layout.FlowLayout;
	import iscript.uiFramework.components.text.Label;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.ContainerEvent;
	
	/**
	 * 固定为Empty布局
	 */
	public class Panel extends UIComponent
	{
	 
		
		private var _header:PanelHeader;
		private var _body:ScrollBox;
		
		//TODO: panel Config
		public function Panel(preferWidth:Number = 300, preferHeight:Number = 300)
		{
			super(preferWidth,preferHeight)
		}
		
		
		public function get body():ScrollBox {
			return _body;
			}
		
		override protected function initialize():void
		{
			_header = new PanelHeader();
			_header.width = _preferWidth;
			//_header.y = 1
			//_header.icon = Bitmap(new icon()).bitmapData;
				
			_body = new ScrollBox(_preferWidth, _preferHeight);
 
			_body.height = _preferHeight -_header.height;
			_body.background = true;
			_body.getBackground().begFill(new BitmapData(5, 5, false, 0xFFFFFF));
			
			_header.width = _body.width;
 			UIComponent(_body).background = true;
			UIComponent(_body).getBackground().repeat = true;
			//UIComponent(_body).getBackground().begFill(Bitmap(new _bg()).bitmapData) 
			border = true;
			getBorder().color = 0x99BBE8;	
			setPosition();
		}
		
		private function setPosition():void {
			    if (_header.visible) {
					 _body.height = _preferHeight - _header.height;
					 _body.y = _header.height;
					}else {
					 _body.height = _preferHeight;
					 _body.y =0
				    }
			}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			_body.render(this);
			_header.render(this);	
			_header.expandButton.ClickHandler = function(e:*):void {
				   _body.visible = !_body.visible;
				   _header.getBorder().bottomVisible = _body.visible
				   if (!_body.visible) {
					    height = _header.height+_header.y
					   }else {
						height = _header.height + _body.height + _header.y;
					   }
				}
			super.render(container);
		}
		
		
		
		override public function set width(value:Number):void
		{
			_header.width = value;
			_body.width = value;
			super.width = value;
		}
		
		override public function get width():Number
		{
			return _body.width;
		}
		
		override public function set height(value:Number):void
		{
			if(_body.visible)
			setPosition();
			super.height = value;
		}
		
		override public function get height():Number
		{
			if (!_body.visible) return _header.height;
			return _preferHeight;
		}
		
		public function get header():PanelHeader 
		{
			return _header;
		}
		
		public function set title(value:String):void 
		{
			if(_header)
			  _header.text = value;
		}
		public function get title():String 
		{
			if(_header)
			return _header.text;
			return "";
		}
		
		
		public function showHeader():void {
			  _header.visible = true;
			setPosition();
			}
			
		public function hideHeader():void {
			 _header.visible = false;
			setPosition();
			}
	
	}

}